﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2021/12/13/周一
 * 时间: 2:33
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WPE
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		// 指向 Program Manager 窗口句柄
	    private IntPtr programIntPtr = IntPtr.Zero;
	
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		public void Init()
	    {
	        // 通过类名查找一个窗口，返回窗口句柄。
	        programIntPtr = Win32.FindWindow("Progman", null);
	
	        // 窗口句柄有效
	        if(programIntPtr != IntPtr.Zero)
	        {   
	
	            IntPtr result = IntPtr.Zero;
	
	            // 向 Program Manager 窗口发送 0x52c 的一个消息，超时设置为0x3e8（1秒）。
	            Win32.SendMessageTimeout(programIntPtr, 0x52c, IntPtr.Zero, IntPtr.Zero, 0, 0x3e8, result);
	
	            // 遍历顶级窗口
	            Win32.EnumWindows((hwnd, lParam) =>
	            {
	                // 找到包含 SHELLDLL_DefView 这个窗口句柄的 WorkerW
	                if (Win32.FindWindowEx(hwnd,IntPtr.Zero, "SHELLDLL_DefView", null) != IntPtr.Zero)
	                {
	                    // 找到当前 WorkerW 窗口的，后一个 WorkerW 窗口。 
	                    IntPtr tempHwnd = Win32.FindWindowEx(IntPtr.Zero, hwnd, "WorkerW", null);
	
	                    // 隐藏这个窗口
	                    Win32.ShowWindow(tempHwnd, 0);
	                }
	                return true;
	            }, IntPtr.Zero);
	        }
	    }
		
	 
		const int WM_CLOSE = 0x0010;
		void ExitToolStripMenuItemClick(object sender, EventArgs e)
		{
			//找到控制台窗口
	        IntPtr cmdIntPtr = Win32.FindWindow("ConsoleWindowClass", null);
			Win32.SendMessage(cmdIntPtr, WM_CLOSE, 0, 0);
		 
			this.Close();
		}
		
		void Button1Click(object sender, EventArgs e)
		{
				// 初始化桌面窗口
	            Init();
	            //找到播放器窗口    
	            IntPtr ffplayIntPtr = Win32.FindWindow("SDL_app", null);
	            //找到控制台窗口
	            IntPtr cmdIntPtr = Win32.FindWindow("ConsoleWindowClass", null);
	            //隐藏控制台窗口
	            Win32.ShowWindow(cmdIntPtr, 0);
	            	
	            // 窗口置父，设置背景窗口的父窗口为 Program Manager 窗口
	            Win32.SetParent(ffplayIntPtr, programIntPtr);
		}
		
		void MainFormResize(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible=false;
                this.notifyIcon1.Visible = true;
			}else{
				
                this.notifyIcon1.Visible = false;
			}
		}
		
		void NotifyIcon1MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.Visible=true;
			this.WindowState =FormWindowState.Normal;
		}
		
		 
		
		void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			//找到控制台窗口
	        IntPtr cmdIntPtr = Win32.FindWindow("ConsoleWindowClass", null);
			Win32.SendMessage(cmdIntPtr, WM_CLOSE, 0, 0);
		 
			 
		}
	}
}
